﻿namespace Chain.DBUtility
{
    using Chain.Common;
    using Chain.Common.DEncrypt;
    using System;
    using System.IO;
    using System.Web;

    public class PubConstant
    {
        public static string ConnectionString
        {
            get
            {
                string text = "";
                //string text = ConfigHelper.GetValue("ConnString");
                //string text = ConfigHelper.GetValueFromData("ConnString");
                System.Xml.XmlDocument xx = new System.Xml.XmlDocument();
                string filePath = System.IO.Path.Combine(HttpRuntime.AppDomainAppPath,"App_Data\\SystemDomain.xml");//HttpContext.Current.Request.MapPath("~/App_Data/SystemDomain.xml");
                if (File.Exists(filePath))
                {
                    xx.Load(filePath);
                    System.Xml.XmlNode item = xx.SelectSingleNode("xml");
                    System.Xml.XmlNode item2 = xx.SelectSingleNode("System");

                    text = item2.SelectSingleNode("ConnString").InnerText.Trim();
                }

                if ((ConfigHelper.GetValue("ConnStringE") == "true") && (text != ""))
                {
                    text = DESEncrypt.Decrypt(text);
                }
                return text;
            }
        }

        public static string DBName
        {
            get
            {
                int startIndex = ConnectionString.IndexOf("database=") + 9;
                int length = ConnectionString.IndexOf(";", startIndex) - startIndex;
                return ConnectionString.Substring(startIndex, length);
            }
        }

        public static string DoMain
        {
            get
            {
                return DESEncrypt.Decrypt(ConfigHelper.GetValue("SystemDomain"));
            }
        }

        public static string DoMainFromData
        {
            get
            {
                //string value = "";
                //System.Xml.XmlDocument xx = new System.Xml.XmlDocument();
                //string filePath = HttpContext.Current.Request.MapPath("~/App_Data/SystemDomain.xml");
                //if (File.Exists(filePath))
                //{
                //    xx.Load(filePath);
                //    System.Xml.XmlNode item = xx.SelectSingleNode("xml");
                //    System.Xml.XmlNode item2 = xx.SelectSingleNode("System");
                    
                //    value = item2.SelectSingleNode("Domain").InnerText.Trim();
                //}
                return DESEncrypt.Decrypt(ConfigHelper.GetValueFromData("Domain"));
            }
        }

        public static string Login
        {
            get
            {
                int startIndex = ConnectionString.IndexOf("uid=") + 4;
                int length = ConnectionString.IndexOf(";", startIndex) - startIndex;
                return ConnectionString.Substring(startIndex, length);
            }
        }

        public static string Pwd
        {
            get
            {
                int startIndex = ConnectionString.IndexOf("pwd=") + 4;
                int length = ConnectionString.IndexOf(";", startIndex) - startIndex;
                return ConnectionString.Substring(startIndex, length);
            }
        }

        public static string ServerIP
        {
            get
            {
                int startIndex = ConnectionString.IndexOf("server=") + 7;
                int length = ConnectionString.IndexOf(";", startIndex) - startIndex;
                return ConnectionString.Substring(startIndex, length);
            }
        }
    }
}

